package com.netdisk.controller;

import com.netdisk.dto.FileDTO;
import com.netdisk.service.FavoriteService;
import com.netdisk.service.FileService;
import com.netdisk.service.UserActivityService;
import com.netdisk.vo.PageResult;
import com.netdisk.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 收藏控制器
 */
@Api(tags = "收藏管理")
@RestController
@RequestMapping("/favorite")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;
    
    @Autowired
    private FileService fileService;
    
    @Autowired
    private UserActivityService userActivityService;

    @ApiOperation("添加收藏")
    @PostMapping("/add")
    public Result<Void> addFavorite(
            @ApiParam(value = "文件ID", required = true) @RequestParam("fileId") Integer fileId,
            @ApiParam(value = "用户ID", required = true) @RequestParam("userId") Integer userId,
            HttpServletRequest request) {
        
        // 先获取文件信息，用于记录活动
        FileDTO fileDTO = fileService.getFileInfo(fileId, userId);
            
        boolean result = favoriteService.addFavorite(fileId, userId);
        
        if (result) {
            // 记录添加收藏活动
            try {
                userActivityService.recordActivity(
                    userId, 
                    fileId, 
                    fileDTO.getName(), 
                    "favorite", 
                    "收藏了文件 \"" + fileDTO.getName() + "\"",
                    request.getRemoteAddr(),
                    request.getHeader("User-Agent")
                );
            } catch (Exception e) {
                // 记录活动失败，但不影响功能
                System.err.println("Failed to record activity: " + e.getMessage());
            }
        }
        
        return result ? Result.success("收藏成功", null) : Result.error("收藏失败");
    }

    @ApiOperation("取消收藏")
    @DeleteMapping("/remove")
    public Result<Void> removeFavorite(
            @ApiParam(value = "文件ID", required = true) @RequestParam("fileId") Integer fileId,
            @ApiParam(value = "用户ID", required = true) @RequestParam("userId") Integer userId,
            HttpServletRequest request) {
        
        // 先获取文件信息，用于记录活动
        FileDTO fileDTO = fileService.getFileInfo(fileId, userId);
            
        boolean result = favoriteService.removeFavorite(fileId, userId);
        
        if (result) {
            // 记录取消收藏活动
            try {
                userActivityService.recordActivity(
                    userId, 
                    fileId, 
                    fileDTO.getName(), 
                    "unfavorite", 
                    "取消收藏文件 \"" + fileDTO.getName() + "\"",
                    request.getRemoteAddr(),
                    request.getHeader("User-Agent")
                );
            } catch (Exception e) {
                // 记录活动失败，但不影响功能
                System.err.println("Failed to record activity: " + e.getMessage());
            }
        }
        
        return result ? Result.success("取消收藏成功", null) : Result.error("取消收藏失败");
    }

    @ApiOperation("获取收藏文件列表")
    @GetMapping("/list")
    public Result<PageResult<FileDTO>> listFavorites(
            @ApiParam(value = "用户ID", required = true) @RequestParam("userId") Integer userId,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(value = "page", defaultValue = "1") Integer page,
            @ApiParam(value = "每页大小", defaultValue = "10") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @ApiParam(value = "搜索关键词", required = false) @RequestParam(value = "query", required = false, defaultValue = "") String query) {
        PageResult<FileDTO> pageResult = favoriteService.listFavorites(userId, page, pageSize, query);
        return Result.success(pageResult);
    }

    @ApiOperation("检查文件是否已收藏")
    @GetMapping("/check")
    public Result<Boolean> isFavorite(
            @ApiParam(value = "文件ID", required = true) @RequestParam("fileId") Integer fileId,
            @ApiParam(value = "用户ID", required = true) @RequestParam("userId") Integer userId) {
        boolean isFavorite = favoriteService.isFavorite(fileId, userId);
        return Result.success(isFavorite);
    }
} 